// pages/payCourse/payCourse.js
var app=getApp();
var util=require("../../utils/util");
var home=require("../../api/home");
var my=require("../../api/my");
var config=require("../../api/config");
var group=require("../../api/group");


Page({
  /*页面的初始数据*/
  data: {
    info: '',
    lessonTime: 0,
    navColor: "#fff",
    title: "报名支付",
    pop_index: 1,
    cou_money: 0,
    pop_show_else: true,
    showAgain: true,
    pop_show: true,
    show_com: 0,
    baby_list: [],
    pref_list: [],
    pref_b_list: [],
    pref_p_list: [],
    pref_text: ['立减券','满减券','礼品券','','平台券'],
    pay_ment: ['微信支付'],
    baby_index: 0,
    cou_index: [],
    pay_index: 0,
    user_msg: '',
    com_data: [],
    ti_cont:["您还有订单未支付","好课程不容错失哦~"],
    latitude: 22.820794,
    longitude: 113.284565,
    token: '',
    userinfo: '',
    pay_money: 0,
    pay_act: true,
    pay_step: 1,
    activity_id: '1344172560662351873',
    timeGroup: '',
    endTime: {hour:'00',minute:'00',second:'00'},
    group_id: '',
    isLeader: 0,
    group_info: '',
    groupType: '',
    is_group: true,
    is_end: false,
    typetexts:['节','月','季','期','年']
  },
  // 监听页面加载
  onLoad: function (options) {
    // console.log('options')
    // console.log(options)
    var that = this;
    // type 1-购买/单独报名 2-定金开团 3-团购价开团 4-定金参团 5-团购价参团
    var id=options.id,lessonTime=options.lessonTime,type=options.type,group_aid=options.group_aid;
    var sysInfo = wx.getSystemInfoSync(),pixelRate = sysInfo.windowWidth / 750;
    var token=wx.getStorageSync('token'),userinfo=wx.getStorageSync('userinfo');
    that.setData({top_height: (sysInfo.statusBarHeight+44)/pixelRate,lessonTime:lessonTime,type:type,group_aid:group_aid,token:token,userinfo:userinfo})
    if(type==4||type==5){
      that.setData({group_id:options.group_id})
    }
    wx.getStorage({
      key: 'city',
      success:function(res){
        var info=res.data;
        that.setData({latitude:info.lat,longitude:info.lng})
        if(type==1){
          that.get_cour_meal_detail(id)
        }else{
          that.get_group_meal_detail(id,type)
        }
      },
      fail:function(){
        if(type==1){
          that.get_cour_meal_detail(id)
        }else{
          that.get_group_meal_detail(id,type)
        }
      },
    })
    // 事件统计
    app.td_app_sdk.event({
      id: 'payCourse',
      label: '课程套餐支付',
      params: {
        key1: "课程套餐支付"
      }
    })
  },
  // 渲染完成
  onReady: function () {
    this.dialog = this.selectComponent('#paypop');
  },
  // 弹窗
  open_paypop:function(e){
    var that = this,com_data=[];
    var num = e.currentTarget.dataset.num;
    if(num == 1){
      com_data=that.data.baby_list
    }else if(num==2){
      com_data.push(that.data.pref_b_list)
      com_data.push(that.data.pref_p_list)
    }else{
      com_data = that.data.pay_ment
    }
    that.setData({show_com:num,pop_show:!that.data.pop_show,com_data:com_data})
  },
  close_pop:function(e){
    var that = this;
    var obj = e.detail.pop_show;
    that.setData({pop_show: obj})
  },
  return:function(){},
  // 选择小孩
  confirm_baby:function(e){
    var that = this;
    var obj = e.detail.res_data;
    that.setData({pop_show: obj.pop_show,baby_index: obj.baby_index})
  },
  // 选择机构券
  check_business:function(e){
    var that = this,pref_list=that.data.com_data,business_money=0,terr_money=0,price=that.data.info.price,cou_money=0,pay_money=0,bid='',pid='',choose_pref=true;
    var cou_index=e.detail;
    if(cou_index[0]!=10000){
      business_money=pref_list[0][cou_index[0]].amount;
      bid=pref_list[0][cou_index[0]].id;
      // if(business_money>price*0.3){
      //   choose_pref=false;
      //   cou_index[1]=10000;
      // }
    }
    if(cou_index.length>1){
      if(cou_index[1]!=10000){
        terr_money=pref_list[1][cou_index[1]].amount;
        pid=pref_list[1][cou_index[1]].id;
      }
    }
    // if(terr_money>0){
    //   if((business_money+terr_money)>=price*0.3){
    //     pay_money=(price*0.7).toFixed(2);
    //     cou_money=(price*0.3).toFixed(2);
    //   }else{
    //     pay_money=(price-business_money-terr_money).toFixed(2);
    //     cou_money=business_money+terr_money
    //   }
    // }else{
    //   if(price-business_money>0){
    //     pay_money=(price-business_money).toFixed(2);
    //     cou_money=business_money
    //   }else{
    //     pay_money=0;
    //     cou_money=price
    //   }
    // }
    if((price-business_money-terr_money)>0){
      pay_money=(price-business_money-terr_money).toFixed(2);
      cou_money=business_money+terr_money
    }else{
      cou_money=price
    }
    // console.log(cou_index,cou_money,pay_money,bid,pid,choose_pref)
    that.setData({cou_index:cou_index,cou_money:cou_money,pay_money:pay_money,bid:bid,pid:pid,choose_pref:choose_pref})
  },
  // 选择平台券
  check_terr:function(e){
    var that=this,pref_list=that.data.com_data,business_money=0,terr_money=0,price=that.data.info.price,cou_money=0,pay_money=0,bid='',pid='';
    var cou_index=e.detail;
    if((cou_index[0]&&cou_index[0]!=10000)||cou_index[0]==0){
      business_money=pref_list[0][cou_index[0]].amount;
      bid=pref_list[0][cou_index[0]].id;
    }
    if(cou_index[1]!=10000){
      terr_money=pref_list[1][cou_index[1]].amount;
      pid=pref_list[1][cou_index[1]].id;
    }
    // if(terr_money>0){
    //   if((business_money+terr_money)>=price*0.3){
    //     pay_money=(price*0.7).toFixed(2);
    //     cou_money=(price*0.3).toFixed(2);
    //   }else{
    //     pay_money=(price-business_money-terr_money).toFixed(2);
    //     cou_money=business_money+terr_money
    //   }
    // }else{
    //   if(price-business_money>0){
    //     pay_money=(price-business_money).toFixed(2);
    //     cou_money=business_money
    //   }else{
    //     pay_money=0;
    //     cou_money=price
    //   }
    // }
    if((price-business_money-terr_money)>0){
      pay_money=(price-business_money-terr_money).toFixed(2);
      cou_money=business_money+terr_money
    }else{
      cou_money=price
    }
    that.setData({cou_index:cou_index,cou_money:cou_money,pay_money:pay_money,bid:bid,pid:pid})
  },
  // 支付方式
  confirm_pay:function(e){
    var that = this;
    var obj = e.detail.res_data;
    that.setData({pop_show: obj.pop_show,pay_index: obj.pay_index})
  },
  // 返回弹窗
  open_check_pop:function(e){
    var that = this;
    // var index = e.detail.index;
    that.setData({pop_show_else: false,pop_show:true})
  },
  nav_back:function(e){
    wx.navigateBack({
      delta: 1,
      fail:function(){
        wx.switchTab({
          url: '../home/home',
        })
      }
    });
  },
  close_pop_else:function(){
    var that = this;
    that.setData({pop_show_else: true})
  },
  // 用户留言
  open_textmsg:function(){
    var that=this,text=that.data.user_msg;
    wx.navigateTo({
      url: '../userMsg/userMsg?text='+text,
    })
    // 事件统计
    app.td_app_sdk.event({
      id: 'userMsg',
      label: '用户留言',
      params: {
        key1: "课程套餐支付"
      }
    })
  },
  // 倒计时
  count_down:function(){
    var that=this,info=that.data.group_info;
    that.data.timeGroup=setInterval(function(){
      var stepTime=(info.groupFound.endTime/1000)-parseInt(new Date().getTime())/1000;
      var endTime=util.time3date(stepTime);
      if(stepTime>0){
        that.setData({endTime:endTime})
      }else{
        clearInterval(that.data.timeGroup)
        that.setData({is_end:true})
      }
    },1000)
  },
  onHide:function(){
    clearInterval(this.data.timeGroup)
  },
  onUnload:function(){
    clearInterval(this.data.timeGroup)
  },
  // 刷新
  onShow:function(){
    var that=this,info=that.data.group_info;
    if(app.globalData.change_baby){
      that.get_baby_list(1);
      app.globalData.change_baby=false;
    }
    if(app.globalData.login){
      var token=wx.getStorageSync('token'),userinfo=wx.getStorageSync('userinfo'),info=that.data.group_info;
      app.globalData.login=false;
      app.globalData.change_dom=false;
      app.globalData.change_table=false;
      that.setData({token:token,userinfo:userinfo})
      that.get_baby_list(1);
      that.get_group_detail();
      if(userinfo.id==info.groupFound.userId){
        that.setData({isLeader:1})
      }
    }
    if(info){
      that.count_down()
    }
  },
  onShareAppMessage:function(e){
    var that=this,info=that.data.info,lessonTime=that.data.lessonTime,type=that.data.type,group_id=that.data.group_id,group_aid=that.data.group_aid,is_group=that.data.is_group,name=that.data.userinfo.nickName;
    if(e.from=="button"){
      if(is_group){
        return{
          title: info.course.name,
          path: '/pages/payCourse/payCourse?id='+info.id+'&lessonTime='+lessonTime+'&type='+type+'&group_id='+group_id+'&group_aid='+group_aid,
          imageUrl: info.course.img
        }
      }else{
        return{
          title: info.course.name,
          path: '/pages/groupCourse/groupCourse?id='+group_aid+'&type=2',
          imageUrl: info.course.img
        }
      }
    }else{
      return{
        title: name+'邀请您一起关注家巴啦',
        path: '/pages/home/home',
        imageUrl: 'https://ossdev0.jiabala.com/xaSpNcZEmBSRd'
      }
    }
  },
  // 参团
  join_group:function(){
    var that=this,type=that.data.type,token=that.data.token,is_group=that.data.is_group,is_end=that.data.is_end;
    if(token){
      clearInterval(that.data.timeGroup)
      if(is_end){
        util.page_show_toast("该拼团活动已过期",2000)
      }else{
        if(is_group){
          if(type==4){
            that.setData({groupType:1})
          }else if(type==5){
            that.setData({groupType:2})
          }
        }else{
          wx.redirectTo({
            url: '../groupCourse/groupCourse?id='+that.data.group_aid+'&type=2',
          })
        }
      }
    }else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  show_toast:function(){
    util.page_show_toast("该拼团活动已过期",2000)
  },
  // 确认支付 (1-购买/单独报名)
  confirm_course:function(e){
    var that=this,info=that.data.info,baby_list=that.data.baby_list,baby_index=that.data.baby_index,user_msg=that.data.user_msg,bid=that.data.bid,pid=that.data.pid,pay_act=that.data.pay_act,pay_step=that.data.pay_step;
    if(baby_list.length==0){
      util.page_show_toast('你还未添加宝宝',1500);
      that.setData({show_com:1,pop_show:!that.data.pop_show,com_data:that.data.baby_list})
      return false;
    }
    var params={
      "childId": baby_list[baby_index].id,
      "childName": baby_list[baby_index].name,
      "courseId": info.courseId,
      "setMealId": info.id,
      "businessId": info.businessId,
      "businessName": info.businessName,
      "businessLogo": info.logoImg,
      "addressDetail": info.businessDetail,
      "courseImg": info.img[0],
      "appId": 1001,
      "courseName": info.courseName,
      "buyerNotes": user_msg
    }
    if(bid||pid){
      params['isUseCoupon']=true
      if(bid){
        params['businessVolumeId']=bid
      }
      if(pid){
        params['platformVolumeId']=pid
      }
    }else{
      params['isUseCoupon']=false
    }
    if(pay_act){
      if(pay_step==1){
        that.create_order(params)
      }else{
        that.pay_order()
      }
    }
    that.setData({pay_act:false})
  },
  // 支付定金 (2/3-开团 4/5-参团)
  confirm_course_hand:function(){
    var that=this,info=that.data.info,baby_list=that.data.baby_list,baby_index=that.data.baby_index,pay_act=that.data.pay_act,type=that.data.type;
    // console.log("pay_act=",pay_act)
    if(baby_list.length==0){
      util.page_show_toast('你还未添加宝宝',1500);
      that.setData({show_com:1,pop_show:!that.data.pop_show,com_data:that.data.baby_list})
      return false;
    }
    var params={
      "activityId": info.course.activityId,
      "childId": baby_list[baby_index].id
    }
    if(pay_act){
      that.get_user_group(params,type)
    }else{
      util.page_show_toast("请稍后再试",2000)
    }
    that.setData({pay_act:false})
  },
  // 获取套餐详情(课程)
  get_cour_meal_detail:function(id){
    wx.showLoading({title: '加载中'})
    var that=this,token=that.data.token;
    var params={
      "id": id,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    home.get_cour_meal_detail(params,token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        info.img=info.img?info.img.split(","):''
        info.score=info.score?info.score.toFixed(1):'5.0';
        info.space=info.distance>1000?(info.distance/1000).toFixed(2):info.distance;
        info.courseNatureName=info.courseNatureName?info.courseNatureName.substr(0,2):''
        that.setData({info:info,pay_money:info.price})
        // 是否获取优惠券
        if(info.price>0){
          that.get_user_b_pref()
        }
        //
        that.get_baby_list(1)
      }
    })
  },
  // 获取套餐详情(拼团)
  get_group_meal_detail:function(id,type){
    wx.showLoading({title: '加载中'})
    var that=this,token=that.data.token;
    var params={
      "id": id,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    group.get_group_meal_detail(params,token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data,course=wx.getStorageSync('cour_details');
        info.img=info.img?info.img.split(","):''
        info.score=info.score?info.score.toFixed(1):'5.0';
        info.space=info.distance>1000?(info.distance/1000).toFixed(2):info.distance;
        info.courseNatureName=info.courseNatureName?info.courseNatureName.substr(0,2):''
        that.setData({pay_money:info.price})
        // console.log('course')
        // console.log(course)
        if(course&&course.setMealId&&course.setMealId==id){
          info.course=course
          that.setData({info:info})
          // 参团详情
          if(type==4||type==5){
            that.get_group_detail()
          }
        }else{
          that.get_cour_and_group(info,type)
        }
        // 开团方式
        if(type==2){
          that.setData({groupType:1})
        }else if(type==3){
          that.setData({groupType:2})
        }
        //
        that.get_baby_list(2)
      }
    })
  },
  // 课程详情
  get_cour_and_group:function(meal_info,type){
    wx.showLoading({title: '加载中'})
    var that=this;
    var params={
      "id": that.data.group_aid,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    group.get_cour_and_group(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        info.course.swipers=info.course.introducePictures?info.course.introducePictures.split(","):'';
        var cour_details={id:info.course.id,img:info.course.swipers[0],name:info.course.name,count:info.course.initialRegistrationCount,isAgeLimit:info.course.isAgeLimit,activityId:info.activityGroup.id,setMealPrice:info.activityGroup.setMealPrice,groupPrice:info.activityGroup.groupPrice,deposit:info.activityGroup.deposit,isDeposit:info.activityGroup.isDeposit,lessonTime:info.course.lessonTime,setMealId:info.activityGroup.setMealId,labelName:info.activityGroup.activityLabelNames};
        wx.setStorageSync('cour_details', cour_details)
        meal_info.course=cour_details;
        // console.log('cour_details')
        // console.log(cour_details)
        that.setData({info:meal_info})
        // 参团详情
        if(type==4||type==5){
          that.get_group_detail()
        }
      }
    })
  },
  // 小孩列表
  get_baby_list:function(type){
    wx.showLoading({title: '加载中'})
    var that=this;
    my.get_baby_list({parentId:that.data.userinfo.id}).then(res=>{
      if(res.data.code==200){
        if(type==2){util.page_show_toast("拼团成功，不可退款",2000)}
        app.globalData.change_baby=false;
        var list=res.data.data;
        if(list.length > 0){
          for(var i=0;i<list.length;i++){
            list[i].birthday=util.formatTime_age(new Date(list[i].birthday))
            var num = (new Date().getFullYear() - new Date(list[i].birthday).getFullYear())>0?(new Date().getFullYear() - new Date(list[i].birthday).getFullYear()):1;
            list[i].age=util.convertToChinaNum(num);
          }
          that.setData({baby_list: list,com_data:list})
        }
      }
    })
  },
  // 可用优惠券列表(机构券)
  get_user_b_pref:function(){
    var that=this,info=that.data.info,token=that.data.token,pref_text=that.data.pref_text;
    var params={
      "courseId": info.courseId,
      "setMealId": info.id,
      "comeFrom": 1,
      "status": 1
    }
    home.get_user_pref(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        var business_money=0,cou_index=[],price=info.price,cou_money=0,pay_money=0,bid='',choose_pref=true;
        if(list.length>0){
          for(var i=0;i<list.length;i++){
            list[i].beginUseTime=util.formatTime_year(new Date(list[i].beginUseTime))
            list[i].expireTime=util.formatTime_year(new Date(list[i].expireTime))
            list[i].couponWayName=pref_text[list[i].type]
          }
          business_money=Math.max.apply(Math,list.map(item => { return item.amount }))
          for(var m=0;m<list.length;m++){
            if(business_money==list[m].amount){
              cou_index[0]=m;
              bid=list[m].id
            }
          }
          // if(business_money>=price*0.3){
          //   choose_pref=false;
          // }
          if((price-business_money)>0){
            pay_money=(price-business_money).toFixed(2);
            cou_money=business_money
          }else{
            cou_money=price
          }
          that.setData({cou_index:cou_index,cou_money:cou_money,pay_money:pay_money,bid:bid})
          that.get_user_t_pref(pay_money,cou_money,choose_pref)
        }else{
          that.get_user_t_pref(price,cou_money,choose_pref)
        }
        that.setData({pref_b_list:list,choose_pref:choose_pref})
      }
    })
  },
  // 可用优惠券列表(平台券)
  get_user_t_pref:function(pay_money,cou_money,choose_pref){
    var that=this,info=that.data.info,token=that.data.token,pref_text=that.data.pref_text,cou_index=that.data.cou_index;
    var pay_money=pay_money,cou_money=cou_money,price=info.price;
    var params={
      "courseId": info.courseId,
      "setMealId": info.id,
      "comeFrom": 2,
      "status": 1
    }
    home.get_user_pref(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        var terr_money=0,pid='';
        if(list.length>0){
          for(var i=0;i<list.length;i++){
            list[i].beginUseTime=util.formatTime_year(new Date(list[i].beginUseTime))
            list[i].expireTime=util.formatTime_year(new Date(list[i].expireTime))
            list[i].couponWayName=pref_text[list[i].type]
          }
          if(choose_pref){
            terr_money=Math.max.apply(Math,list.map(item => { return item.amount }))
            for(var n=0;n<list.length;n++){
              if(terr_money==list[n].amount){
                cou_index[1]=n;
                pid=list[n].id
              }
            }
            // if((cou_money+terr_money)>=price*0.3){
            //   pay_money=(price*0.7).toFixed(2);
            //   cou_money=(price*0.3).toFixed(2);
            // }else{
            //   pay_money=(pay_money-terr_money).toFixed(2);
            //   cou_money=cou_money+terr_money
            // }
            if((pay_money-terr_money)>0){
              pay_money=(pay_money-terr_money).toFixed(2);
              cou_money=cou_money+terr_money
            }else{
              pay_money=0
              cou_money=price
            }
          }
          that.setData({cou_index:cou_index,cou_money:cou_money,pay_money:pay_money,pid:pid})
        }
        that.setData({pref_p_list:list})
      }
    })
  },
  // 生成订单
  create_order:function(params){
    wx.showLoading({title: ''})
    var that=this,baby_list=that.data.baby_list,baby_index=that.data.baby_index,token=that.data.token;
    home.create_order(params,token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({orderId:info.id,order_price: info.paidInAmount,pref_money:(info.businessDiscountAmount+info.platformDiscountAmount),orderNo:info.orderNo,pay_step:2})
        if(info.paymentStatus==1){
          if(info.paidInAmount>0){
            that.pay_order()
          }else{
            that.payment_order()
          }
        }else if(info.paymentStatus==2){
          if(info.reverseStatus==1){
            wx.showToast({
              title: '亲，该课程您的'+baby_list[baby_index].name+'宝宝已购买，当前机构正在排课中，暂不能购买，请稍等哦！',
              icon: 'none',
              duration: 5000
            })
          }else if(info.reverseStatus==2||info.reverseStatus==3){
            wx.showToast({
              title: '亲，该课程您的'+baby_list[baby_index].name+'宝宝已购买，并为你排好了课，在课程表中等你查看哟~',
              icon: 'none',
              duration: 5000
            })
          }
        }
      }else{
        that.setData({pay_act:true})
        util.page_show_toast(res.data.message,2000);
      }
    }).catch(res=>{
      that.setData({pay_act:true})
    })
  },
  // 支付订单
  pay_order:function(){
    var that=this,orderId=that.data.orderId,order_price=that.data.order_price,pref_money=that.data.pref_money,info=that.data.info,token=that.data.token,activity_id=that.data.activity_id,orderNo=that.data.orderNo,payStep=that.data.type,group_aid=that.data.group_aid;
    var params={
      "orderNo": orderNo,
      "payType": 3,
      "openId":app.globalData.openId?app.globalData.openId:wx.getStorageSync('openId')
    }
    home.pay_order(params,token).then(res=>{
      if(res.data.code==200){
        wx.hideLoading({})
        var data=JSON.parse(res.data.data);
        // 微信支付
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success:function(res){
            wx.redirectTo({
              url: '../paySuccess/paySuccess?type=1&index=1&price='+order_price+'&money='+pref_money+'&aid='+activity_id+'&payStep='+payStep+'&orderId='+orderId+'&group_aid='+group_aid,
            })
            that.get_count_after()
          },
          fail:function(res){
            // util.page_show_toast('支付失败',1500);
            that.setData({pay_act:true,pay_step:2})
          }
        })
      }else{
        that.setData({pay_act:true})
      }
    })
  },
  // 0元订单支付
  payment_order:function(){
    var that=this,token=that.data.token,orderNo=that.data.orderNo,info=that.data.info;
    var params={"orderNo": orderNo,"payType": 5}
    home.payment_order(params,token).then(res=>{
      if(res.data.code==200){
        wx.redirectTo({
          url: '../paySuccess/paySuccess?type=1&index=0',
        })
        if(info.price>0){
          that.get_count_after()
        }
      }else{
        that.setData({pay_act:true})
      }
    })
  },
  // 重置抽奖次数
  get_count_after:function(){
    var that=this,token=that.data.token,activity_id=that.data.activity_id;
    var params={
      "activityId": activity_id
    }
    config.get_count_after(params,token).then(res=>{

    })
  },
  // 用户是否有拼团(开团和参团)
  get_user_group:function(params,type){
    wx.showLoading({title: ''})
    // pay_step 1-未生成订单 2-已生成订单  type 2/3-开团 4/5-参团
    var that=this,token=that.data.token,pay_step=that.data.pay_step;
    group.get_user_group(params,token).then(res=>{
      // console.log("type=",type)
      if(res.data.code==200){
        if(!res.data.data){
          if(type==2||type==3){
            if(pay_step==1){
              that.create_goup_order()
            }else{
              that.pay_order()
            }
          }else{
            that.isGroup()
          }
        }else{
          that.setData({pay_act:true})
          util.page_show_toast('您当前小孩已有正在进行的拼团',2000)
        }
      }
    })
  },
  // 生成拼团订单
  create_goup_order:function(){
    var that=this,info=that.data.info,baby_list=that.data.baby_list,baby_index=that.data.baby_index,user_msg=that.data.user_msg,group_id=that.data.group_id,token=that.data.token,type=that.data.type,orderType=1;
    if(type==3||type==5){
      orderType=3
    }
    var params={
      "activityId": info.course.activityId,
      "childId": baby_list[baby_index].id,
      "buyerNotes": user_msg,
      "childName": baby_list[baby_index].name,
      "courseImg": info.img[0],
      "appId": 1001,
      "orderType": orderType,
      "groupFoundId": group_id,
      "groupType": group_id?2:1
    }
    if(!group_id){delete params.groupFoundId}
    group.create_goup_order(params,token).then(res=>{
      if(res.data.code==200){
        wx.hideLoading({})
        var info=res.data.data;
        // console.log("order=",type)
        that.setData({orderId:info.id,order_price:info.paidInAmount,pref_money:0,orderNo:info.orderNo,pay_step:2})
        //
        that.pay_order()
      }else{
        that.setData({pay_act:true})
        util.page_show_toast(res.data.message,2000);
      }
    })
  },
  // 单个团详情
  get_group_detail:function(){
    var that=this,info=that.data.info,group_id=that.data.group_id,userinfo=that.data.userinfo;
    var params={
      "activityId": info.course.activityId,
      "groupId": group_id
    }
    group.get_group_detail(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({group_info:info})
        if(userinfo){
          if(userinfo.id==info.groupFound.userId){
            that.setData({isLeader:1})
          }
        }
        if(info.groupFound.surplusCount<1){
          that.setData({is_group:false})
        }
        // 参团倒计时
        that.count_down()
      }
    })
  },
  // 支付前判断是否已成团(参团判断)
  isGroup:function(){
    var that=this,pay_step=that.data.pay_step;
    var params={
      "id": that.data.group_id
    }
    group.isGroup(params).then(res=>{
      if(res.data.code==200){
        if(pay_step==1){
          that.create_goup_order()
        }else{
          that.pay_order()
        }
      }else{
        that.setData({pay_act:true})
        util.page_show_toast(res.data.message,2000)
      }
    })
  },
})